//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//********************************************************* 

using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.TridentWordAddIn.ResourceHelper;
using Timer = System.Timers.Timer;

namespace Microsoft.TridentWordAddIn.Presentation
{
    public partial class DownloadAnimationUserControl : UserControl
    {
        private Bitmap[] animationImages;
        private int currentImageIndex;
        private bool isUpload;
        private System.Timers.Timer timer = new Timer();

        /// <summary>
        /// Initializes a new instance of the <see cref="DownloadAnimationUserControl"/> class.
        /// </summary>
        public DownloadAnimationUserControl()
        {
            InitializeComponent();
            this.components = new System.ComponentModel.Container();
            components.Add(this.timer);
            this.animationImages = new Bitmap[]
                {
                    (Bitmap)ResourceFetcher.GetImage("Animated_Download_Pipeline1"),
                    (Bitmap)ResourceFetcher.GetImage("Animated_Download_Pipeline2"),
                    (Bitmap)ResourceFetcher.GetImage("Animated_Download_Pipeline3"),
                    (Bitmap)ResourceFetcher.GetImage("Animated_Download_Pipeline4"),
                    (Bitmap)ResourceFetcher.GetImage("Animated_Download_Pipeline5"),
                    (Bitmap)ResourceFetcher.GetImage("Animated_Download_Pipeline6"),
                    (Bitmap)ResourceFetcher.GetImage("Animated_Download_Pipeline7")
                };
            this.SetAnimationImage(0);
            this.timer.Interval = 150;
            this.timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is upload.
        /// </summary>
        /// <value><c>true</c> if this instance is upload; otherwise, <c>false</c>.</value>
        [Browsable(true)]
        [DefaultValue(false)]
        public bool IsUpload
        {
            get { return this.isUpload; }
            set { this.isUpload = value; }
        }

        private void SetAnimationImage(int index)
        {
            this.pictureBox1.Image = this.animationImages[index];
        }

        /// <summary>
        /// Switches the image.
        /// </summary>
        private void SwitchImage()
        {
            if (this.isUpload)
            {
                if (this.currentImageIndex < 0)
                {
                    this.currentImageIndex = this.animationImages.Length - 1;
                }

                this.SetAnimationImage(this.currentImageIndex);
                this.currentImageIndex--;
            }
            else
            {
                if (this.currentImageIndex > this.animationImages.Length - 1)
                {
                    this.currentImageIndex = 0;
                }

                this.SetAnimationImage(this.currentImageIndex);
                this.currentImageIndex++;
            }
       }

       /// <summary>
       /// Elapseds this instance.
       /// </summary>
        public void Elapsed()
        {
            this.SwitchImage();
        }

        /// <summary>
        /// Starts this instance.
        /// </summary>
        public void Start()
        {
            this.timer.Start();
        }

        /// <summary>
        /// Stops this instance.
        /// </summary>
        public void Stop()
        {
            this.timer.Stop();
        }

        /// <summary>
        /// Handles the Elapsed event of the timer control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Timers.ElapsedEventArgs"/> instance containing the event data.</param>
        private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            this.Invoke(new MethodInvoker(this.Elapsed));
        }
    }
}
